// miniprogram/pages/addlose/addloseMessage.js
var toast = require('../../utils/toastUtil.js')
//const worker = wx.createWorker('workers/TimeConsuming.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lossTitle: '',
    lossNum: "",
    lossPrice: "",
    lossObj: "",
    index: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.lossObj != undefined) {
      var loss = JSON.parse(options.lossObj)
      this.setData({
        lossTitle: loss.losstitle,
        lossNum: loss.lossNum,
        lossPrice: loss.lossPrice,
        index: options.index
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setNavigationBarTitle({
      title: "添加损失信息"
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    console.log("页面隐藏onhide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
     //worker.terminate
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  saveDate: function(res) {
    if (this.data.lossTitle.length == 0 || this.data.lossNum == 0 || this.data.lossPrice == 0) {
      toast.showToast("请完善信息")
      return
    }
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2]
    if (this.data.index == -1) {
      prevPage.setData({
        lossName: prevPage.data.lossName.concat({
          losstitle: this.data.lossTitle,
          lossNum: this.data.lossNum,
          lossPrice: this.data.lossPrice
        })
      })
      // 在主线程的代码 addlosseMessage 中初始化 Worker
      // this.workerDada()
    } else {
      prevPage.data.lossName[this.data.index].losstitle = this.data.lossTitle;
      prevPage.data.lossName[this.data.index].lossNum = this.data.lossNum;
      prevPage.data.lossName[this.data.index].lossPrice = this.data.lossPrice;
    }
    wx.navigateBack({
      delta: 1
    })
  },
  focusName: function(res) {
    this.data.lossTitle = res.detail.value
  },
  focusNum: function(res) {
    this.data.lossNum = res.detail.value
  },
  focuPrice: function(res) {
    this.data.lossPrice = res.detail.value
  },
  workerDada:function(){
    const worker = wx.createWorker('workers/TimeConsuming.js')
    worker.postMessage({
      msg: "主线程向子线程发送消息"
    })
    worker.onMessage(function (res) {
      console.log("主线程中收到的消息", res)
    })
  }
})